package com.jadyn.ai.medialearn.encode

import android.graphics.BitmapFactory
import android.graphics.SurfaceTexture
import android.os.Bundle
import android.os.Environment
import android.os.Handler
import android.os.HandlerThread
import androidx.appcompat.app.AppCompatActivity
import android.text.TextUtils
import android.util.Log
import android.view.TextureView
import android.widget.Toast
import androidx.lifecycle.lifecycleScope
import com.jadyn.ai.medialearn.R
import com.jadyn.mediakit.video.encode.VideoEncoder
import kotlinx.android.synthetic.main.activity_encode_frame.*
import kotlinx.coroutines.launch
import java.io.File

/**
 *@version:
 *@FileDescription: 将帧编码为视频
 *@Author:Jing
 *@Since:2019/4/2
 *@ChangeList:
 */
class EncodeFrameActivity : AppCompatActivity() {

    // 测试文件夹
    private val encodePicDir = TextUtils.concat(Environment.getExternalStorageDirectory().path,
            "/0000").toString()


    private val thread by lazy {
        val handlerThread = HandlerThread("encodeFrame")
        handlerThread.start()
        handlerThread
    }

    private val handler by lazy {
        Handler(thread.looper)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_encode_frame)
        frame_texture_view.surfaceTextureListener = object : TextureView.SurfaceTextureListener {
            override fun onSurfaceTextureSizeChanged(surface: SurfaceTexture?, width: Int, height: Int) {
            }

            override fun onSurfaceTextureUpdated(surface: SurfaceTexture?) {
            }

            override fun onSurfaceTextureDestroyed(surface: SurfaceTexture?): Boolean {
                return false
            }

            override fun onSurfaceTextureAvailable(surface: SurfaceTexture?, width: Int, height: Int) {

            }
        }

        handler.post {
            Log.d("fuck", "thread ${Thread.currentThread().name} ")
            val startTime = System.currentTimeMillis()
            val videoEncoder = VideoEncoder(720, 1280, 1800000,
                    24)
            videoEncoder.start(Environment.getExternalStorageDirectory().path
                    + "/encodeyazi640${videoEncoder.bitRate}.webm")
            val file = File(encodePicDir)
            file.listFiles()
                .filter { it.isFile && it.extension.lowercase() in listOf("jpg", "png") }
                .sortedWith(compareBy { extractNumber(it.name) }).forEachIndexed { index, it ->
                Log.d("fuck", " file name ${it.absolutePath}  path is ${it.path}")
                BitmapFactory.decodeFile(it.path)?.apply {
                    videoEncoder.drainFrame(this, index)
                }
            }
            videoEncoder.drainEnd()
            val endTime = System.currentTimeMillis()
            Log.d("fuck","drainVideoTime=${endTime - startTime}")
            lifecycleScope.launch {
                Toast.makeText(this@EncodeFrameActivity,"编码完成",Toast.LENGTH_SHORT).show()
            }
        }
    }

    private fun extractNumber(filename: String): Int {
        return Regex("""\d+""").find(filename)?.value?.toIntOrNull() ?: 0
    }
}


